#include "Core.h"
#include "WinHelper.h"
using namespace std;

///////////////////////////////////////////////////////////////////
//Codes

#define CMD_ABOUT 501
#define CMD_TRAINING 502
#define CMD_RESULTS 503
#define CMD_HELP 504
#define CMD_EXIT 505

///////////////////////////////////////////////////////////////////
//Prototypes

void InitWindows();
void ReleaseWindows();
void ShowMainWindow();
void HideMainWindow();

void LoadLibs();
void FlushLibs();

///////////////////////////////////////////////////////////////////
//Structures of data

struct Windows
{
	HWND MainWindow;
	HWND TrainWindow;
	HWND Results;
	HWND Help;
	HWND About;
} w;

struct MainWindowElements
{
	HWND ButtonAbout;
	HWND ButtonTraining;
	HWND ButtonResults;
	HWND ButtonHelp;
	HWND ButtonExit;

	HWND LeftImage;
} mwe;

struct TrainingWindowElements
{

} twe;

struct ResultWindowElements
{

} rwe;

struct HelpWindowElements
{

} hwe;

struct AboutWindowElements
{
    HWND LinkGroup;
} awe;

///////////////////////////////////////////////////////////////////
//App

void InitApp()
{
    ZeroMemory(&w, sizeof(w));
    InitWindows();
    LoadLibs();
    ShowMainWindow();
}

void ReleaseApp()
{

}

///////////////////////////////////////////////////////////////////
//Events

bool EventCloseWindow(HWND hWnd)
{
	ShowWindow(hWnd, SW_HIDE);
	if (hWnd != NULL && w.MainWindow == hWnd)
		return true;
	return false;
}

bool EventCommand(HWND hWnd, WPARAM wParam)
{
	switch (wParam)
	{
	case CMD_ABOUT:
		ShowWindow(w.About, SW_SHOW);
		break;
	case CMD_EXIT:
		PostQuitMessage(0);
		break;
	default:
        return false;
	}
    return true;
}

///////////////////////////////////////////////////////////////////
//Windows Functions

void InitMainWindow();
void InitAboutWindow();
void InitHelpWindow();
void InitTrainWindow();
void InitResultWindow();

void InitWindows()
{
	InitMainWindow();
	InitAboutWindow();
	InitHelpWindow();
	InitTrainWindow();
	InitResultWindow();
}

DWORD style = WS_CAPTION | WS_SYSMENU | WS_POPUPWINDOW | WS_MINIMIZEBOX;
DWORD chv_style = WS_VISIBLE | WS_CHILD;
DWORD simple_button_style = BS_TEXT | BS_CENTER;

void InitMainWindow()
{
	int width = 500, height = 350,
		posx, posy;
	CenterToScreen(width, height, posx, posy);
	HINSTANCE hInst = (HINSTANCE) GetModuleHandleW(NULL);
	w.MainWindow =
		CreateWindowExW(0, L"stdclass", L"鉛筆 — Enpitsu", style,
						posx, posy, width, height, NULL, NULL, hInst, NULL);
	RECT client_rect;
	GetClientRect(w.MainWindow, &client_rect);
	int btn_width = 200, btn_heigh = 30;
	/////////////////////////////////////////
	int btn_about_posx = client_rect.right - btn_width - 30;
	int btn_about_posy = 30;

	mwe.ButtonAbout =
    CreateWindowExW(0, L"button", L"Об Авторе", simple_button_style | chv_style,
					btn_about_posx, btn_about_posy, btn_width, btn_heigh,
					w.MainWindow, (HMENU) CMD_ABOUT, hInst, NULL);
	SetDefaultFont(mwe.ButtonAbout);
	/////////////////////////////////////////
    int btn_tr_posx = btn_about_posx;
    int btn_tr_posy = btn_about_posy + btn_heigh + 30;

	/////////////////////////////////////////
	UpdateWindow(w.MainWindow);
	SetDefaultFont(w.MainWindow);
}
//
void InitAboutWindow()
{
	int width = 400, height = 300,
		posx, posy;
	CenterToScreen(width, height, posx, posy);
	w.About =
		CreateWindowExW(0, L"stdclass", L"Об Авторе", style,
						posx, posy, width, height, w.MainWindow, NULL, GetModuleHandleW(NULL), NULL);
	UpdateWindow(w.About);
	SetDefaultFont(w.About);
}

void InitHelpWindow()
{

}

void InitTrainWindow()
{

}

void InitResultWindow()
{

}

void ReleaseWindows()
{
	if (w.MainWindow)
	{
		DestroyWindow(w.MainWindow);
	}
}

void ShowMainWindow()
{
    ShowWindow(w.MainWindow, SW_SHOW);
}

void HideMainWindow()
{
	ShowWindow(w.MainWindow, SW_HIDE);
}

///////////////////////////////////////////////////////////////////
//Libs functions

void LoadLibs()
{

}

void FlushLibs()
{

}
